from loctor_4399 import *
from selenium import webdriver
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from time import sleep
# 解耦
"""
方便代码后期维护
比如说页面元素发生了更改，只需要修改element这一个文件：loctor_4399，后续代码无需改变
"""
class Play4399:
    def __init__(self,driver,url):
        """
        :param driver: 传入driver
        :param url: 传入url
        """
        self.driver = driver
        self.url = url
        self.driver.maximize_window()
        self.driver.get(self.url)
        self.driver.implicitly_wait(15)
    def searchConetnt(self,text):
        # 输入4399
        WebDriverWait(self.driver,10,0.5).until(EC.presence_of_element_located(input_4399)).send_keys(text)
        # 点击百度一下
        WebDriverWait(self.driver,10,0.5).until(EC.presence_of_element_located(click_searchBtn)).click()

    def clickA(self):
        # 点击出现的第一个链接
        WebDriverWait(self.driver,10,0.5).until(EC.presence_of_element_located(click_OneA)).click()
        # 句柄切换
        self.driver.switch_to.window(self.driver.window_handles[-1])
    def clickBingHuo(self):
        # 点击冰火人
        WebDriverWait(self.driver,10,0.5).until(EC.presence_of_element_located(click_BingHuo)).click()
        # 句柄切换
        self.driver.switch_to.window(self.driver.window_handles[-1])
        # 点击第一个冰火人游戏
        WebDriverWait(self.driver,10,0.5).until(EC.presence_of_element_located(click_OneBingHuo)).click()
        # 句柄切换
        self.driver.switch_to.window(self.driver.window_handles[-1])
        # 点击开始游戏
        WebDriverWait(self.driver,10,0.5).until(EC.presence_of_all_elements_located(clickPlayGame))[1].click()
        # 最后执行完，强制等待
        sleep(15)
if __name__ == '__main__':
    # 实例化驱动
    driver = webdriver.Chrome()
    url = "https://www.baidu.com"
    # 通过Play4399类创建对象
    browser = Play4399(driver,url)
    # 输入4399内容
    browser.searchConetnt('4399')
    # 点击第一个链接
    browser.clickA()
    # 点击冰火人
    browser.clickBingHuo()















